package cn.ichiva.tushare.analysis.data;

import cn.ichiva.tushare.analysis.Config;
import cn.ichiva.tushare.analysis.data.国内宏观.利率数据.Shibor;
import cn.ichiva.tushare.sdk.common.DateUtils;
import cn.ichiva.tushare.sdk.tushare.TuClient;
import cn.ichiva.tushare.sdk.tushare.TuDataCacheAble;
import cn.ichiva.tushare.sdk.tushare.entity.TuRequest;
import cn.ichiva.tushare.sdk.tushare.entity.TuTable;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Optional;

/**
 * 日线行情
 *
 * 交易日每天15点～16点之间。本接口是未复权行情，停牌期间不提供数据。
 * 获取2000年到现在的所有数据
 */
@Slf4j
public class Daily extends TuDataCacheAble {
    private static final String KEY_SUFFIX = "time";

    public Daily(TuClient client) {
        super(client);
    }

    public Optional<TuTable> get(String ts_code){
        TuRequest request = new TuRequest();
        request.setApi("daily");
        request.setDescribe("交易日每天15点～16点之间。本接口是未复权行情，停牌期间不提供数据。");

        HashMap<String,String> paramsMap = new HashMap<>();
        paramsMap.put("ts_code",ts_code);
        paramsMap.put("start_date","20140101");
        paramsMap.put("end_date", DateUtils.lastTUDateString());
        request.setParams(paramsMap);

        LinkedHashMap<String,String> fieldMap = new LinkedHashMap<>();
        fieldMap.put("ts_code","股票代码");
        fieldMap.put("trade_date","交易日期");
        fieldMap.put("open","开盘价");
        fieldMap.put("high","最高价");
        fieldMap.put("low","最低价");
        fieldMap.put("close","收盘价");
        fieldMap.put("pre_close","昨收价");
        fieldMap.put("change","涨跌额");
        fieldMap.put("pct_chg","涨跌幅 （未复权，如果是复权请用通用行情接口）");
        fieldMap.put("vol","成交量 （手）");
        fieldMap.put("amount","成交额 （千元）");


        request.setFieldMap(fieldMap);
        setRequest(request);

        return super.get();
    }

    @Deprecated
    @Override
    public Optional<TuTable> get() {
        return super.get();
    }

    public static void main(String[] args) {
        new Daily(new TuClient(Config.url, Config.token))
                .get("000001.SZ").ifPresent(System.out::println);
    }
}
